package com.ihemou.gateway.filter;

import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.stp.SaLoginModel;
import com.ihemou.common.core.constant.Constants;
import com.ihemou.common.redis.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author saperliu
 * @version 1.0.0
 * @date 2021-09-11 8:00
 * @Description 侦听器的实现
 */
@Component
public class GatewayListener implements SaTokenListener {

    private static final Logger log = LoggerFactory.getLogger(GatewayListener.class);

    @Autowired
    private RedisService redisService;

    /**
     * 每次登录时触发
     */
    @Override
    public void doLogin(String loginType, Object loginId, SaLoginModel loginModel) {
        log.info(" login   {}  {}  {}", loginType, loginId, loginModel);

        redisService.setCacheObject(Constants.USER_LOGIN_IDS+":"+loginId,loginId);
    }

    /**
     * 每次注销时触发
     */
    @Override
    public void doLogout(String loginType, Object loginId, String tokenValue) {
        log.info(" logout   {}  {}  {}", loginType, loginId, tokenValue);
        redisService.deleteObject(Constants.USER_LOGIN_IDS+":"+loginId);
        redisService.deleteObject(Constants.USER_LOGIN_USER_INFO+":"+loginId);
    }

    /**
     * 每次被踢下线时触发
     */
    @Override
    public void doLogoutByLoginId(String loginType, Object loginId, String tokenValue, String device) {
        log.info(" LogoutByLoginId   {}  {}  {}  {} ", loginType, loginId, tokenValue, device);
        redisService.deleteObject(Constants.USER_LOGIN_IDS+":"+loginId);
        redisService.deleteObject(Constants.USER_LOGIN_USER_INFO+":"+loginId);
    }

    /**
     * 每次被顶下线时触发
     */
    @Override
    public void doReplaced(String loginType, Object loginId, String tokenValue, String device) {
        log.info(" Replaced   {}  {}  {}", loginType, loginId, tokenValue);
        redisService.deleteObject(Constants.USER_LOGIN_IDS+":"+loginId);
        redisService.deleteObject(Constants.USER_LOGIN_USER_INFO+":"+loginId);
    }

    /**
     * 每次被封禁时触发
     */
    @Override
    public void doDisable(String loginType, Object loginId, long disableTime) {
        log.info(" Disable   {}  {}  {}", loginType, loginId, disableTime);
        redisService.deleteObject(Constants.USER_LOGIN_IDS+":"+loginId);
        redisService.deleteObject(Constants.USER_LOGIN_USER_INFO+":"+loginId);
    }

    /**
     * 每次被解封时触发
     */
    @Override
    public void doUntieDisable(String loginType, Object loginId) {
        log.info(" UntieDisable   {}  {} ", loginType, loginId);
        redisService.deleteObject(Constants.USER_LOGIN_IDS+":"+loginId);
        redisService.deleteObject(Constants.USER_LOGIN_USER_INFO+":"+loginId);
    }

    /**
     * 每次创建Session时触发
     */
    @Override
    public void doCreateSession(String id) {
        log.info(" Session  create   {}", id);
    }

    /**
     * 每次注销Session时触发
     */
    @Override
    public void doLogoutSession(String id) {
        log.info(" Session  logout  {}", id);
    }
}
